package com.wizz.trade.information.controller;

import com.wizz.trade.common.PageVo;
import com.wizz.trade.information.dto.StofDto;
import com.wizz.trade.information.dto.page.StofPageDto;
import com.wizz.trade.information.entity.Stof;
import com.wizz.trade.information.service.StofService;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.information.vo.StofVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 资料模块/仓储费
 */
@RestController
@RequestMapping("/stof")
public class StofController {

    @Resource
    private StofService stofService;

    /**
     * 添加操作
     *
     * @param stofDto
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAuthority('仓储协议、费率登记')")

    public ResponseResult addStof(@RequestBody StofDto stofDto) {
        Stof stof = new Stof();
        BeanUtils.copyProperties(stofDto,stof);
        stofService.addStof(stof);
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 修改（编辑）操作
     *
     * @param stofDto
     * @param id
     * @return
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('仓储协议、费率登记')")

    public ResponseResult updateStof(@RequestBody StofDto stofDto, @PathVariable("id") Integer id) {

        Stof stof = new Stof();
        BeanUtils.copyProperties(stofDto,stof);
        stof.setId(id);
        stofService.updateStof(stof);
        return new ResponseResult<>(StatusCode.OK, "修改成功");
    }

    /**
     * 删除（撤销）操作
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('仓储协议、费率登记')")

    public ResponseResult deleteStof(@PathVariable("id") Integer id) {
        stofService.deleteStof(id);
        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }

    /**
     * 分页刷新、条件查询
     * @param stofPageDto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('仓储协议、费率登记')")

    public ResponseResult<PageVo<StofVo>> list(StofPageDto stofPageDto){
        PageVo<Stof> rawPage = stofService.getPage(stofPageDto);
        List<Stof> data = rawPage.getData();
        List<StofVo> dataVo = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            StofVo  stofVo = new StofVo();
            BeanUtils.copyProperties(data.get(i), stofVo);
            dataVo.add(stofVo);
        }
        return new ResponseResult<>(StatusCode.OK, new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));
    }
}
